package org.karelun.rabbit.rcp.property.tab;

import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.karelun.rabbit.graph.GraphPackage;
import org.karelun.rabbit.graph.Rectangled;

public class RectanglePropertySection extends BasePropertySection<Rectangled> {

	private Text xText;
	
	private Text yText;
	
	private Text heightText;
	
	private Text widthText;
	
	private ISWTObservableValue xTextObserver;

	private ISWTObservableValue yTextObserver;
	
	private ISWTObservableValue hTextObserver;
	
	private ISWTObservableValue wTextObserver;
	
	public RectanglePropertySection() {
	}
	
	@Override
	protected String getSectionTitle() {
		return "Rectangle";
	}
	
	@Override
	protected void doCreateControl(Composite parent) {
		parent.setLayout(new GridLayout(2,true));
		parent.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		createLocatinPropertyArea(parent);
		creteSizePropertyArea(parent);
	}

	private void createLocatinPropertyArea(Composite composite) {
		Composite leftComp = new Composite(composite, SWT.NONE);
		leftComp.setLayout(new GridLayout(2, false));
		leftComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		new Label(leftComp, SWT.NONE).setText("X:");
		xText=new Text(leftComp, SWT.BORDER);
		xText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		xTextObserver=WidgetProperties.text(SWT.Modify).observe(xText);
		
		new Label(leftComp, SWT.NONE).setText("Y:");
		yText=new Text(leftComp, SWT.BORDER);
		yText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		yTextObserver=WidgetProperties.text(SWT.Modify).observe(yText);
	}

	private void creteSizePropertyArea(Composite composite) {
		Composite leftComp = new Composite(composite, SWT.NONE);
		leftComp.setLayout(new GridLayout(2, false));
		leftComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		new Label(leftComp, SWT.NONE).setText("Height:");
		heightText=new Text(leftComp, SWT.BORDER);
		heightText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		hTextObserver=WidgetProperties.text(SWT.Modify).observe(heightText);
		
		new Label(leftComp, SWT.NONE).setText("Width:");
		widthText=new Text(leftComp, SWT.BORDER);
		widthText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		wTextObserver=WidgetProperties.text(SWT.Modify).observe(widthText);
	}
	
	@Override
	protected void dataBinding() {
		Rectangled rect = getSelectedObject();
		dataBinding(xTextObserver, rect,GraphPackage.Literals.RECTANGLED__X,Integer.class);
		dataBinding(yTextObserver, rect,GraphPackage.Literals.RECTANGLED__Y,Integer.class);
		dataBinding(hTextObserver, rect,GraphPackage.Literals.RECTANGLED__HEIGHT,Integer.class);
		dataBinding(wTextObserver, rect,GraphPackage.Literals.RECTANGLED__WIDTH,Integer.class);
	}
	
	@Override
	public void refresh() {
	}
}
